﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace NCRGEA.UserControls
{
    public partial class ucPayments : UserControl
    {
        private int _MemberID;
        public int MemberID
        {
            get
            {
                return _MemberID;
            }
            set
            {
                _MemberID = value;
                RefreshData();
            }
        }

        private bool _IsEditable;
        public bool IsEditable
        {
            get
            {
                return _IsEditable;
            }
            set
            {
                _IsEditable = value;
                UpdateEditable();
            }
        }

        private void UpdateEditable()
        {
            btnAddPayment.Enabled = _IsEditable;
        }

        public ucPayments()
        {
            InitializeComponent();
            RefreshData();
        }

        public void RefreshData()
        {
            string sqlQry = @"SELECT
	                            p.Date,
	                            pm.Description,
	                            '$' + CAST(CAST(ROUND(p.Amount,2) as NUMERIC(10,2)) as VARCHAR) Amount,
	                            p.ReferenceNumber,
                                uA.FirstName + ' ' + uA.LastName StaffName
                            FROM
	                            payments p
	                            LEFT JOIN
		                            payMethods pm
	                            ON
		                            p.MethodID=pm.MethodID
                                LEFT JOIN
                                    usrAccounts uA
                                ON
                                    p.StaffID=uA.StaffID
                            WHERE
	                            p.MemberID=@MemberID
                            ORDER BY
	                            p.Date DESC";
            SqlParameter[] sParam = new SqlParameter[1];
            sParam[0] = new SqlParameter("@MemberID", _MemberID) { SqlDbType = SqlDbType.Int };
            DataTable result = AAL.DAL.ExecuteQuery(sqlQry, sParam);
            if (result == null || result.Rows.Count == 0)
                return;
            grdPayments.DataSource = result;
        }

        private void btnAddPayment_Click(object sender, EventArgs e)
        {
            this.Parent.Controls.Add(new ucPayments_Add() { MemberID = _MemberID, Dock=DockStyle.Fill});
            this.Parent.Controls.Remove(this);
        }
    }
}
